package cn.fl.custpay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.livecheck.dto.CmmLiveChkDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.custpay.api.ICmmIntelChdOrderApiService;
import cn.fl.custpay.api.ICmmIntelOrderApiService;
import cn.fl.custpay.condition.CmmIntelChdOrderQC;
import cn.fl.custpay.dto.CmmIntelChdOrderDTO;
import cn.fl.custpay.dto.CmmIntelOrderDTO;
import cn.fl.custpay.service.ICmmIntelOrderService;
import cn.fl.custpay.transform.CmmIntelChdOrderVOTransform;
import cn.fl.custpay.transform.CmmIntelOrderVOTransform;
import cn.fl.custpay.vo.CmmIntelChdOrderVO;
import cn.fl.custpay.vo.CmmIntelOrderVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysDictionaryService;
import fl.constant.facade.framework.constant.CmmCustPayConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * TODO 智能收款订单记录表 客户端服务层接口实现
 * TODO 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 * @package: cn.fl.custpay.service.impl
 * @className: CmmIntelOrderServiceImpl
 * @author: shijibin@lionbridgecapital.cn
 * @createDate: 2019/3/19 14:57
 * @version: V1.0.0
 * @copyright: (c) 2019
 * @updateRemark: [序号][YYYY-MM-DD] [更改人姓名][变更描述]
 */
@Service
public class CmmIntelOrderServiceImpl implements ICmmIntelOrderService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CmmIntelOrderServiceImpl.class);
	
	/**
	 * 智能收款父订单API服务
	 */
	@Autowired
	private ICmmIntelOrderApiService cmmIntelOrderApiService;

	/**
	 * 智能收款子订单API服务
	 */
	@Autowired
	private ICmmIntelChdOrderApiService cmmIntelChdOrderApiService;

	/**
	 *  数据字典服务
	 */
	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Override
	public CmmIntelOrderVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		CmmIntelOrderDTO dto = null;//this.自定义ApiService.selectCmmIntelOrderDTOById(id);
		return CmmIntelOrderVOTransform.toVO(dto);
	}

	@Override
	public Long add(CmmIntelOrderVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//return this.cmmIntelOrderApiService.add(CmmIntelOrderVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//return this.cmmIntelOrderApiService.removeCmmIntelOrderDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(CmmIntelOrderVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		//return this.cmmIntelOrderApiService.modifyCmmIntelOrderDTO(CmmIntelOrderVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}

		//数据字典集合
		List<String> codeGrpCdList = new ArrayList<>();
		//总订单状态
		codeGrpCdList.add(CmmCustPayConstant.CMM_INTEL_ORDER_STS_CD);
		codeGrpCdList.add(CmmCustPayConstant.CMM_INTEL_BUS_TYP_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> orderStsList = dicMap.get(CmmCustPayConstant.CMM_INTEL_ORDER_STS_CD);
        List<KeyValueVO> busTypList = dicMap.get(CmmCustPayConstant.CMM_INTEL_BUS_TYP_CD);
		Map<String, String> orderStsMap = null;
		if (CollectionUtils.isNotEmpty(orderStsList)) {
			orderStsMap = new HashMap<>(orderStsList.size());
			for (KeyValueVO dto : orderStsList) {
				orderStsMap.put(dto.getKey(), dto.getValue());
			}
		}
        Map<String, String> busStsMap = null;
        if (CollectionUtils.isNotEmpty(busTypList)) {
            busStsMap = new HashMap<>(busTypList.size());
            for (KeyValueVO dto : busTypList) {
                busStsMap.put(dto.getKey(), dto.getValue());
            }
        }
		Pager pager = this.cmmIntelOrderApiService.searchListPage(page);
		List<CmmIntelOrderDTO> list = (List<CmmIntelOrderDTO>) pager.getResultList();
		List<CmmIntelOrderVO> cmmIntelOrderVOS = CmmIntelOrderVOTransform.toVOList(list);
		if(CollectionUtils.isNotEmpty(cmmIntelOrderVOS)){
			for(CmmIntelOrderVO cmmIntelOrderVO : cmmIntelOrderVOS){
				if (orderStsMap != null && orderStsMap.containsKey(cmmIntelOrderVO.getOrderStsCd())) {
					cmmIntelOrderVO.setOrderStsNm(orderStsMap.get(cmmIntelOrderVO.getOrderStsCd()));
				}
                if (busStsMap != null && busStsMap.containsKey(cmmIntelOrderVO.getBusTypeCd())) {
                    cmmIntelOrderVO.setBusTypeNm(busStsMap.get(cmmIntelOrderVO.getBusTypeCd()));
                }

				//金额分转元
				cmmIntelOrderVO.setOrderAmtNm(DecimalUtils.convertFenToYuan(cmmIntelOrderVO.getOrderAmt() == null ? 0L : cmmIntelOrderVO.getOrderAmt()));
				cmmIntelOrderVO.setSumMoneyNm(DecimalUtils.convertFenToYuan(cmmIntelOrderVO.getSumMoney() == null ? 0L : cmmIntelOrderVO.getSumMoney()));
			}
		}
		pager.setResultList(cmmIntelOrderVOS);
		return pager;
	}

	@Override
	public List<CmmIntelChdOrderVO> searchChdListByParentId(Long parOrderId) throws BaseException {

		//数据字典集合
		List<String> codeGrpCdList = new ArrayList<>();
        //分解状态
		codeGrpCdList.add(CmmCustPayConstant.CMM_INTEL_DECOM_STS_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> decomStsList = dicMap.get(CmmCustPayConstant.CMM_INTEL_DECOM_STS_CD);
		Map<String, String> decomStsMap = null;
		if (CollectionUtils.isNotEmpty(decomStsList)) {
			decomStsMap = new HashMap<>(decomStsList.size());
			for (KeyValueVO dto : decomStsList) {
				decomStsMap.put(dto.getKey(), dto.getValue());
			}
		}

		List<CmmIntelChdOrderDTO> cmmIntelChdOrderDTOS = this.cmmIntelChdOrderApiService.searchListByParentId(parOrderId);

		List<CmmIntelChdOrderVO> cmmIntelChdOrderVOS = CmmIntelChdOrderVOTransform.toVOList(cmmIntelChdOrderDTOS);
		if(CollectionUtils.isNotEmpty(cmmIntelChdOrderVOS)){
			for(CmmIntelChdOrderVO cmmIntelChdOrderVO : cmmIntelChdOrderVOS){
				if (decomStsMap != null && decomStsMap.containsKey(cmmIntelChdOrderVO.getIsDecsucCd())) {
					cmmIntelChdOrderVO.setIsDecsucNm(decomStsMap.get(cmmIntelChdOrderVO.getIsDecsucCd()));
				}
				//系统只会同步成功的订单，所以订单状态默认为“成功”
				cmmIntelChdOrderVO.setOrderStsNm("成功");
				//金额分转元
				cmmIntelChdOrderVO.setOrderAmtNm(DecimalUtils.convertFenToYuan(cmmIntelChdOrderVO.getOrderAmt() == null ? 0L : cmmIntelChdOrderVO.getOrderAmt()));
			}
		}
		return cmmIntelChdOrderVOS;
	}
}
